package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.mapper.CompanyInfoMapper;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.CompanyInfoVo;
import com.zhongke.cotrun.model.pojo.LevelOneComment;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.LevelOneCommentService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.math.BigInteger;


@RequestMapping("/LevelOneComment")
@RestController
public class LevelOneCommentController {
    @Autowired
    LevelOneCommentService levelOneCommentService;

    @Autowired
    CompanyInfoMapper companyInfoMapper;

    /**
     * 新增一级评论
     *
     * @param levelOneComment
     * @return
     */
    @RequestMapping("/insertLevelOneComment")
    public APIResponse insertLevelOneComment(LevelOneComment levelOneComment) throws MyException {
        WebUserEntity userEntity = ShiroUtils.getWebUser();//用户信息
        if (userEntity == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }
        levelOneComment.setLinkUserId(userEntity.getUserId().intValue());

        if("1".equals(userEntity.getType())){
            levelOneComment.setLinkUserName(userEntity.getRealName());
            levelOneComment.setLinkUserAccountHead(userEntity.getAccountHead());
        }else {
            CompanyInfoVo c =companyInfoMapper.selectCompanyInfoByUserId(userEntity.getUserId().intValue());
            levelOneComment.setLinkUserName(c.getCompanyName());
            levelOneComment.setLinkUserAccountHead(c.getCompanyLogo());
        }


        LevelOneComment levelOneComment1=levelOneCommentService.insertLevelOneComment(levelOneComment);
        if (levelOneComment1!=null) {
            return APIResponse.returnSuccess(levelOneComment1);
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);
    }

    /**
     * 根据项目id查看一级列表
     *
     * @param projectId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectLevelOneComment")
    public APIResponse selectLevelOneComment(@NotNull(message = "项目id不能为空") Integer projectId,String type, @RequestParam(required = false, defaultValue = "1") Integer pageNumber, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        return APIResponse.returnSuccess(levelOneCommentService.selectLevelOneCommentByprojetId(projectId,type ,pageNumber, pageSize));

    }

    /**
     * 根据id查看一级评论
     *
     * @param id
     * @return
     */
    @GetMapping("/selectLevelOneCommentByid")
    public APIResponse selectLevelOneCommentByid(BigInteger id,String type) {

        return APIResponse.returnSuccess(levelOneCommentService.selectLevelOneCommentById(id,type));
    }

    /**
     * 删除评论(逻辑删除)
     * @param levelOneCommentId
     * @return
     * @throws MyException
     */

    @GetMapping("/delectLevelOneCommentByid")
    public APIResponse delectLevelOneCommentByid(Integer levelOneCommentId) throws MyException {

        WebUserEntity userEntity = ShiroUtils.getWebUser();//用户信息
        if (userEntity == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }
        if (levelOneCommentService.updateStateLevelOneComment(1, levelOneCommentId, userEntity.getUserId().intValue())) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
    }

}
